package com.infore.metadata.core.enums;

/**
 * 数据库枚举
 *
 * @author ：lijh
 * @date 创建时间： 2022/12/19 15:49
 */
public enum DatabaseEnum {

    /**
     *
     */
    ORACLE(1, "oracle", "Oracle数据库", "oracle.jdbc.OracleDriver", "jdbc:oracle:thin:@IP地址:端口号/数据库名", false),
    MYSQL(2, "mysql", "MySQL数据库", "com.mysql.cj.jdbc.Driver", "jdbc:mysql://IP地址:端口号/数据库名?serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true&useSSL=false&allowMultiQueries=true&allowPublicKeyRetrieval=true&useInformationSchema=true&characterEncoding=utf-8", false),
    MYSQL5(2, "mysql", "MySQL数据库", "com.mysql.jdbc.Driver", "jdbc:mysql://IP地址:端口号/数据库名?serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true&useSSL=false&allowMultiQueries=true&allowPublicKeyRetrieval=true&useInformationSchema=true&characterEncoding=utf-8", false),
    MARIADB(3, "mariadb", "MariaDB数据库", "org.mariadb.jdbc.Driver", "jdbc:mariadb://IP地址:端口号/数据库名", true),
    SQLSERVER2005(4, "sqlserver2005", "SQLServer2005数据库", "com.microsoft.sqlserver.jdbc.SQLServerDriver", "jdbc:sqlserver://IP地址:端口号;DatabaseName=数据库名", true),
    SQLSERVER2012(5, "sqlserver2012", "SQLServer2012数据库", "com.microsoft.sqlserver.jdbc.SQLServerDriver", "jdbc:sqlserver://IP地址:端口号;DatabaseName=数据库名", true),
    POSTGRESQL(6, "postgresql", "Postgre数据库", "org.postgresql.Driver", "jdbc:postgresql://IP地址:端口号/数据库名", true),
    HSQLDB(7, "hsqldb", "HSQL数据库", "org.hsqldb.jdbcDriver", "jdbc:hsqldb:hsql://localhost", true),
    DB2(8, "db2", "DB2数据库", "com.ibm.db2.jcc.DB2Driver", "jdbc:db2://IP地址:端口号/数据库名:progressiveStreaming=2", true),
    SQLITE(9, "sqlite", "SQLite数据库", "org.sqlite.JDBC", "jdbc:sqlite:/path/db-file-name (MAC以及Linux) jdbc:sqlite://path/db-file-name (Windows)", true),
    H2(10, "h2", "H2数据库", "org.h2.Driver", "jdbc:h2:db-file-name/数据库名", true),
    DM(11, "dm", "达梦数据库", "dm.jdbc.driver.DmDriver", "jdbc:dm://IP地址:端口号/数据库名", true),
    XUGU(12, "xugu", "虚谷数据库", "com.xugu.cloudjdbc.Driver", "jdbc:xugu://IP地址:端口号/数据库名", true),
    KINGBASE(13, "kingbase", "人大金仓数据库", "com.kingbase8.Driver", "jdbc:kingbase8://IP地址:端口号/数据库名", true),
    PHOENIX(14, "phoenix", "Phoenix HBase数据库", "org.apache.phoenix.jdbc.PhoenixDriver", "jdbc:phoenix", true),
    HIGHGO(15, "highgo", "瀚高数据库", "com.highgo.jdbc.Driver", "jdbc:highgo://IP地址:端口号/数据库名", true),
    ;


    /**
     * 数据库类型代码，自定义代码
     */
    private Integer code;
    /**
     * 数据库名称-英文 oracle
     */
    private String name;
    /**
     * 数据库名称-中文
     */
    private String nameChina;
    /**
     * 驱动类，比如 com.mysql.cj.jdbc.Driver
     */
    private String driverClass;
    /**
     * 请求URL，比如 jdbc:oracle:thin:@IP地址:端口号/数据库名
     */
    private String url;
    /**
     * 数据库类型是否可以使用,true 禁用， false 不禁用
     */
    private Boolean disabled;

    /**
     * 根据数据库代码查询对应的枚举
     * @param code 数据库代码
     * @return
     */
    public static DatabaseEnum getByCode(Integer code) {
        DatabaseEnum[] databaseEnums = values();
        for (DatabaseEnum databaseEnum : databaseEnums) {
            if (databaseEnum.getCode().equals(code)) {
                return databaseEnum;
            }
        }
        return null;
    }

    /**
     * 判断数据源驱动是否存在
     * @return true 存在该类型驱动，false 不存在
     */
    public boolean driveExist(){
        try {
            Class.forName(driverClass);
            return true;
        } catch (ClassNotFoundException e) {
            return false;
        }
    }

    DatabaseEnum(Integer code, String name, String nameChina, String driverClass, String url, Boolean disabled) {
        this.code = code;
        this.name = name;
        this.nameChina = nameChina;
        this.driverClass = driverClass;
        this.url = url;
        this.disabled = disabled;
    }

    public String getDriverClass() {
        return driverClass;
    }

    public String getUrl() {
        return url;
    }

    public Integer getCode() {
        return code;
    }

    public Boolean getDisabled() {
        return disabled;
    }

    public String getName() {
        return name;
    }

    public String getNameChina() {
        return nameChina;
    }



}
